package com.treetao.union.mall.api.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.treetao.union.core.exception.DevMException;
import com.treetao.union.db.SettingConvertService;
import com.treetao.union.db.model.CommonUser;
import com.treetao.union.db.pojo.SettingTypeWeixin;
import com.treetao.union.db.service.CommonUserService;

import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;

@Service
public class TencentDbService {

	@Autowired
	private SettingConvertService settingConvertService;
	
	@Autowired
	private CommonUserService commonUserService;

	/**
	 * 获取微信配置
	 * 
	 * @return
	 */
	public SettingTypeWeixin getConfigTencentWeixin() {
		SettingTypeWeixin weixin = settingConvertService.getWeixinSetting();
		if (weixin == null) {
			throw new DevMException("请先配置微信");
		}
		return weixin;
	}

	/**
	 * 用户是否已存在
	 * 
	 * @param appid
	 * @param openid
	 * @return
	 */
	public boolean existUser(String appid, String openid) {
		QueryWrapper<CommonUser> q = new QueryWrapper<CommonUser>();
		q.eq("wxopenid", openid);
		return commonUserService.count(q) > 0;
	}

	/**
	 * 保存用户
	 * 
	 * @param appid
	 * @param info
	 * @return
	 */
	public boolean saveUser(String appid, WxMaUserInfo info) {
		QueryWrapper<CommonUser> q = new QueryWrapper<CommonUser>();
		q.eq("wxopenid", info.getOpenId());
		CommonUser user = commonUserService.getOne(q);
		boolean isUpdate = true;
		if (user == null) {
			user = new CommonUser();
			user.setRegtime(new Date());
			isUpdate = false;
		}
		//user.setCity(info.getCity());
		//user.setProvince(info.getProvince());
		//user.setCountry(info.getCountry());
		user.setSex(info.getGender());
		user.setUsername(info.getNickName());
		user.setPhoto(info.getAvatarUrl());
		user.setWxopenid(info.getOpenId());
		user.setWxunionid(info.getUnionId());
		if (isUpdate) {
			return commonUserService.updateById(user);
		} else {
			return commonUserService.save(user);
		}
	}
	
	public Long checkAndUpdateWxUser(String appid, String openid, String sessionKey) {
		QueryWrapper<CommonUser> q =new QueryWrapper<CommonUser>();
		q.eq("wxopenid", openid);
		CommonUser user = commonUserService.getOne(q);
		boolean isUpdate = true;
		if (user == null) {
			user = new CommonUser();
			user.setWxopenid(openid);
			isUpdate = false;
		} 
		user.setSessionkey(sessionKey);
		if (!isUpdate) {
			user.setRegtime(new Date());
			commonUserService.save(user);
		} else {
			commonUserService.updateById(user);
		}
		return user.getId();
	}

	public CommonUser getUserInfo(Long userId) {
		CommonUser user = commonUserService.getById(userId);
		if(user==null) {
			return null;
		}
		user.setDelFlag(null);
		user.setPhone(null);
		user.setRegtime(null);
		user.setSessionkey(null);
		user.setWxopenid(null);
		user.setWxunionid(null);
		return user;
	}
	
	public String getUserSessionKey(String appid, String openid) {
		QueryWrapper<CommonUser> q = new QueryWrapper<CommonUser>();
		q.eq("wxopenid", openid);
		q.select("sessionkey");
		CommonUser user = commonUserService.getOne(q);
		return user == null ? null : user.getSessionkey();
	}
}
